package address;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class PwEdit {

	public static int edit() {

		Scanner input = new Scanner(System.in);

		String pw;
		String npw;
		boolean pw_check = false;
		boolean check = true;
		boolean check2 = true;

		while (check) {

			System.out.print("비밀번호를 입력하세요 : ");
			pw = input.next();
			pw_check = pw_check(pw);

			if (pw_check) {
				do {
					System.out.println("변경하고자 하는 비밀번호를 입력해주세요");
					npw = input.next();

					if (npw.length() >= 8) {
						System.out.println("8자 미만으로 입력해주세요");
					} else
						check2 = false;

				} while (check2);

				String check_npw = npw;

				while (check) {

					System.out.println("다시 한번 입력해주세요");
					String check_npw2 = input.next();

					if (check_npw.equals(check_npw2)) {
						update_pw(check_npw);

						check = false;
						System.out.println("비밀번호 변경 완료!");
					} else {
						System.out.println("비밀번호가 일치하지 않습니다.");
					}
				}
			} else
				System.out.println("비밀번호가 일치하지 않습니다.");
		}

		System.out.println("메인화면으로 돌아가려면 아무키나 눌러주세요");

		if (input.next() != null) {
			return 0;
		}

		input.close();

		return 0;
	}

	static boolean update_pw(String check_npw) {// 실제 입력받은 값을 업데이트 하는 것.

		Connection conn = null;
		Statement stmt;
		boolean result = false;

		Scanner input = new Scanner(System.in);
		try {

			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager
					.getConnection(
							"jdbc:mysql://localhost:3306/jmk?useUnicode=true&characterEncoding=utf-8",
							"root", "websys");
			stmt = conn.createStatement();
			stmt.execute("UPDATE user SET pw='" + check_npw
					+ "' where id='user'");
			result = true;

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// TODO Auto-generated method stub
		return result;
	}

	static boolean pw_check(String npw) {// 입력받은 값과 일치하는지 체크하는 함수
		Connection conn = null;
		Statement stmt;
		boolean result = false;

		Scanner input = new Scanner(System.in);
		try {

			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager
					.getConnection(
							"jdbc:mysql://localhost:3306/jmk?useUnicode=true&characterEncoding=utf-8",
							"root", "websys");
			stmt = conn.createStatement();

			ResultSet rs = stmt
					.executeQuery("SELECT * FROM user where id ='user'");
			rs.next();
			String input_pw = rs.getString("pw");

			if (npw.equals(input_pw)) {
				result = true;
			} else
				result = false;

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

}
